package com.spring.sunflower.conversation

import android.content.Intent
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.makeramen.roundedimageview.RoundedImageView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.spring.sunflower.R
import com.spring.sunflower.anchor.PersonalDetailActivity
import com.spring.sunflower.base.BaseActivity
import com.spring.sunflower.bean.LatestVisitorBean
import com.spring.sunflower.bean.LatestVisitorNumBean
import com.spring.sunflower.constant.Constant
import kotlinx.android.synthetic.main.activity_latest_visitor.*

/**
 * Created by Kevin on 2021/8/24<br/>
 *
 * Describe:<br/>
 */
class LatestVisitorActivity : BaseActivity<LatestVisitorPresenter>(),
    ConversationContract.LatestVisitorView, OnRefreshListener, OnLoadMoreListener {
    private var recyclerView: RecyclerView? = null
    private var tvTotalVisitor: TextView? = null
    private var tvTodayVisitor: TextView? = null
    private var tvYesterdayVisitor: TextView? = null
    private var pageIndex = 1
    private var rivAvatar: RoundedImageView? = null
    private var mAdapter: LatestVisitorAdapter? = null
    private var mData: MutableList<LatestVisitorBean.DatasBean> = ArrayList()
    private var refreshLayout:SmartRefreshLayout?=null
    override fun initView() {
        val avatar = getField(Constant.KEY_PHOTO, "")
        tvTitle.text = "最近访客"
        recyclerView = findViewById(R.id.rvRecyclerView)
        tvTodayVisitor = findViewById(R.id.tvTodayVisitor)
        tvTotalVisitor = findViewById(R.id.tvVisitorTotal)
        tvYesterdayVisitor = findViewById(R.id.tvYesterdayVisitor)
        rivAvatar = findViewById(R.id.rivAvatar)
        refreshLayout = findViewById(R.id.refreshLayout)
        val linearLayoutManager = LinearLayoutManager(this)
        recyclerView!!.layoutManager=linearLayoutManager
        mAdapter = LatestVisitorAdapter(R.layout.adapter_item_visitor,mData)
        recyclerView!!.adapter=mAdapter

        Glide.with(this)
            .load(avatar)
            .placeholder(R.drawable.ic_placeholder)
            .into(rivAvatar!!)
        refreshLayout!!.setOnRefreshListener(this)
        refreshLayout!!.setOnLoadMoreListener(this)
        mPresenter.getLatestVisitorTotal()
        mPresenter.getLatestVisitor(pageIndex.toString(), "kwd")
        mAdapter!!.addChildClickViewIds(R.id.rivAvatar)
        mAdapter!!.setOnItemClickListener { adapter, view, position ->
            val datasBean = mData[position]
            val intent = Intent(this, PersonalDetailActivity::class.java)
            intent.putExtra(
                Constant.KEY_TARGET_ID,
                datasBean.memberId.toString()
            )
            startActivity(intent)
        }

    }

    override fun getLayoutID(): Int = R.layout.activity_latest_visitor

    override fun setPresenter(): LatestVisitorPresenter = LatestVisitorPresenter(this)

    override fun showLatestVisitor(data: List<LatestVisitorBean.DatasBean>) {
        if (pageIndex == 1) {
            mAdapter!!.setList(data)
        }else{
            mAdapter!!.addData(data)
        }
        if (refreshLayout!!.isRefreshing) {
            refreshLayout!!.finishRefresh()
        }
        if (refreshLayout!!.isLoading) {
            refreshLayout!!.finishLoadMore()
        }

    }

    override fun showLatestVisitorTotal(data: LatestVisitorNumBean.DatasBean) {
        tvTotalVisitor?.text = data.totalCount
        tvYesterdayVisitor?.text = data.yesterdayCount
        tvTodayVisitor?.text = data.todayCount
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        pageIndex=1
        mPresenter.getLatestVisitor(pageIndex.toString(), "kwd")
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPresenter.getLatestVisitor((++pageIndex).toString(), "kwd")
    }

}